package com.sqpoll.android.poll;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.sqpoll.android.R;

/**
 * The adapter for poll list at home.
 * 
 * @author Younggue Bae
 */
public class HomePollListAdapter extends BaseAdapter {
	
	public static final String TAG = "HomePollListAdapter";
	
	Context mContext;
	LayoutInflater mInflater;
	int mLayoutResourceId;
	private List<Poll> mRowList = new ArrayList<Poll>();
	private float mImageWidth;
	
	public HomePollListAdapter(Context context, int layoutResourceId) {
		this(context, layoutResourceId, new ArrayList<Poll>());
	}

	public HomePollListAdapter(Context context, int layoutResourceId, List<Poll> items) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
		this.mLayoutResourceId = layoutResourceId;
		this.mRowList = items;

		@SuppressWarnings("deprecation")
		float width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();

		/*
		float margin = (int) AndroidUtil.convertDpToPixel(10f, (Activity) context);
		// two images, three margins of 1dips
		mImageWidth = ((width - (3 * margin)) / 2);
		*/
		mImageWidth = (width / 2);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item_poll_small, null);
		
			//holder.mImageUserProfile = (ImageView) convertView.findViewById(R.id.image_user_profile);			
			holder.mImagePoll = (ImageView) convertView.findViewById(R.id.image_poll);
			holder.mUsername = (TextView) convertView.findViewById(R.id.username);
			holder.mCreateDate = (TextView) convertView.findViewById(R.id.create_date);
			holder.mStatus = (TextView) convertView.findViewById(R.id.status);
			holder.mQuestion = (TextView) convertView.findViewById(R.id.question);
			holder.mCommentCount = (TextView) convertView.findViewById(R.id.comment_count);
			holder.mVoterCount = (TextView) convertView.findViewById(R.id.voter_count);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		Poll item = (Poll) getItem(position);
		
		//holder.mImageUserProfile.setImageDrawable(item.getUserProfileImage());
		holder.mImagePoll.setImageDrawable(item.getPollImage());
		setImageBitmap(item.getPollImage(), holder.mImagePoll);
		
		holder.mUsername.setText(item.getUsername());
		holder.mCreateDate.setText(item.getCreateDate());
		holder.mStatus.setText(item.getStatus());
		holder.mQuestion.setText("               " + item.getQuestion());
		holder.mCommentCount.setText(String.valueOf(item.getCommentCount()));
		holder.mVoterCount.setText(String.valueOf(item.getVoterCount()));
		
		return convertView;
	}

	@Override
	public int getCount() {
		return mRowList.size();
	}

	@Override
	public Object getItem(int position) {
		return mRowList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addItem(Poll item) {
		mRowList.add(item);
	}
	
	public void addItems(List<Poll> items) {
		mRowList.addAll(items);
	}

	/**
	 * Resizes the image proportionately so it fits the entire space.
	 */
	private void setImageBitmap(Drawable d, ImageView imageView) {
		BitmapDrawable drawable = (BitmapDrawable) d;
		Bitmap bitmap = drawable.getBitmap();

		float i = ((float) mImageWidth) / ((float) bitmap.getWidth());
		float imageHeight = i * (bitmap.getHeight());
		TableRow.LayoutParams params = (TableRow.LayoutParams) imageView.getLayoutParams();
		params.height = (int) imageHeight;
		params.width = (int) mImageWidth;
		imageView.setLayoutParams(params);
		imageView.setImageDrawable(d);
	}
	
	/**
	 * View holder for a list item.
	 * 
	 */
	class ViewHolder {

		//ImageView mImageUserProfile;
		ImageView mImagePoll;
		TextView mUsername;
		TextView mCreateDate;
		TextView mStatus;
		TextView mQuestion;
		TextView mCommentCount;
		TextView mVoterCount;
	}

}
